package com.sgcc.fj.pmos.common.utils;

import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.sgcc.fj.pmos.common.bean.PackageBean;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品对比工具
 */
public class PackageContrastUtils {

    /**
     * 加入对比
     *
     * @param bean
     */
    public static void addContrast(PackageBean bean) {
        if (!exists(bean.getCombId())) {
            List<PackageBean> list = getPackageContrastList();
            list.add(bean);
            String json_result = GsonUtils.toJson(list);
            SPUtils.getInstance().put(ArguConstant.PKLIST, json_result);
        } else {
            ToastUtils.showShort("该商品已在对比列表,请勿重复添加！");
        }
    }

    /**
     * 删除对比
     *
     * @param combId 套餐ID
     */
    public static void deletContrast(String combId) {
        List<PackageBean> list = getPackageContrastList();
        for (int i = 0; i < list.size(); i++) {
            if (combId.equals(list.get(i).getCombId())) {
                list.remove(i);
            }
        }
        String json_result = GsonUtils.toJson(list);
        SPUtils.getInstance().clear();
        SPUtils.getInstance().put(ArguConstant.PKLIST, json_result);
    }

    /**
     * 根据guid判断是否已经存在
     *
     * @param combId
     * @return
     */
    public static boolean exists(String combId) {
        if (TextUtils.isEmpty(combId)) {
            return false;
        }
        List<PackageBean> list = getPackageContrastList();
        for (PackageBean packageBean : list) {
            if (combId.equals(packageBean.getCombId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取套餐对比列表
     *
     * @return
     */
    public static List<PackageBean> getPackageContrastList() {
        List<PackageBean> list;
        String json = SPUtils.getInstance().getString(ArguConstant.PKLIST);
        if (TextUtils.isEmpty(json)) {
            list = new ArrayList<>();
        } else {
            Type type = new TypeToken<List<PackageBean>>() {
            }.getType();
            list = GsonUtils.fromJson(json, type);
        }
        return list;
    }

    /**
     * 清除对比数据
     *
     * @return
     */
    public static void clear() {
        SPUtils.getInstance().clear();
    }


    /**
     * 加入足迹
     *
     * @param bean
     */
    public static void addFootprint(PackageBean bean) {
        if (!existsFootprint(bean.getCombId())) {
            List<PackageBean> list = getPackageFootprintList();
            if (list.size() == 20) {
                list.remove(0);
            }
            list.add(bean);
            String json_result = GsonUtils.toJson(list);
            SPUtils.getInstance().put(ArguConstant.FOOTPRINT_LIST, json_result);
        }
    }

    /**
     * 获取足迹列表
     *
     * @return
     */
    public static List<PackageBean> getPackageFootprintList() {
        List<PackageBean> list;
        String json = SPUtils.getInstance().getString(ArguConstant.FOOTPRINT_LIST);
        if (TextUtils.isEmpty(json)) {
            list = new ArrayList<>();
        } else {
            Type type = new TypeToken<List<PackageBean>>() {
            }.getType();
            list = GsonUtils.fromJson(json, type);
        }
        return list;
    }


    /**
     * 根据guid判断是否已经存在 足迹
     *
     * @param combId
     * @return
     */
    public static boolean existsFootprint(String combId) {
        if (TextUtils.isEmpty(combId)) {
            return false;
        }
        List<PackageBean> list = getPackageFootprintList();
        for (PackageBean packageBean : list) {
            if (combId.equals(packageBean.getCombId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 删除足迹
     *
     * @param combId 套餐ID
     */
    public static void deleteFootprint(String combId) {
        List<PackageBean> list = getPackageFootprintList();
        for (int i = 0; i < list.size(); i++) {
            if (combId.equals(list.get(i).getCombId())) {
                list.remove(i);
            }
        }
        String json_result = GsonUtils.toJson(list);
        SPUtils.getInstance().clear();
        SPUtils.getInstance().put(ArguConstant.FOOTPRINT_LIST, json_result);
    }


}
